import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";
const express = require("express");
const router = express.Router();
import r from "../r";
import * as config from "../../config";

router.post(
  "/addBug",
  async (req: Request, res: Response, next: NextFunction) => {
    const { email, url, title, review, sys, img, browser } = req.body;

    if (process.env.NODE_ENV === "eh") {
      res.json({
        code: 200,
        data: undefined,
        msg: "success"
      });
    }

    try {
      const newurl = `${config.api}/bug/submitBug`;
      // const newurl = `http://192.168.30.23:5000/bug/submitBug`;

      const ret = await r(
        {
          method: "POST",
          url: newurl,
          body: JSON.stringify({
            browser,
            img_ids: img,
            ops_sys: sys,
            recur_step: review,
            title,
            url,
            username: email
          })
        },
        req,
        res
      );

      const jsonRet = JSON.parse(ret.body);

      res.json(jsonRet);
    } catch (e) {
      res.json({
        code: -1,
        data: undefined,
        msg: e
      });
    }
  }
);

export default router;
